//
//  YSPBaseViewController.swift
//  Weibo Demo
//
//  Created by 朴裕识 on 15/12/16.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit

class YSPBaseViewController: UITableViewController, YSPVisitorViewDelegate {
    
    var userlogin = YSPUserAccount.userLogin
//    var userlogin = false
    override func loadView() {
        userlogin ? super.loadView() : setupVisitorView()
    }
    
    /**
     设置访客视图
     */
    func setupVisitorView() {
        
        view = visitorView
        
        //指定代理
        visitorView.delegate = self
        
        //设置导航栏左右按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
        
        
        //其他tabBar按钮继承了这个基类，创建的时候也会有这些方法，所以这里打印self，结果就是tabBar按钮的控制器
        //然后我们就能在这里通过这个self操作，比如设置图片
        if self is YSPDiscoverController {
            visitorView.setBackgroundImageAndLabel("visitordiscover_image_message", text: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        } else if self is YSPHomeController {
            //设置转轮旋转动画开始
            visitorView.startRotationAnimation()
            //程序退出前台或者回到前台都会发通知，在这里接收通知，调用方法，旋转图片
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)

        } else if self is YSPMessageController {
            visitorView.setBackgroundImageAndLabel("visitordiscover_image_message", text: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        } else if self is YSPProfileController {
            visitorView.setBackgroundImageAndLabel("visitordiscover_image_profile", text: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
    }
    
    func didEnterBackground() {
        visitorView.stopAnimation()
    }
    
    func didBecomeActive() {
        visitorView.resumeAnimation()
    }
    
    lazy var visitorView: YSPVisitorView = YSPVisitorView()
    
    //MARK: - 代理方法
    func doWhenLoginBtnDidClicked() {
        let OauthVC = YSPOauthController()
        presentViewController(UINavigationController(rootViewController: OauthVC), animated: true, completion: nil)
    }
    
    func doWhenRegisterBtnDidClicked() {
        print(__FUNCTION__)
    }
}
